package com.igoal.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.igoal.entity.DataStatus;
import com.igoal.entity.Goal;
import com.igoal.entity.MustDoGoal;

@Repository(value="mustDoGoalDAO")
public class MustDoGoalDAOImpl extends GenericDaoHibernate<MustDoGoal, Long>
		implements MustDoGoalDAO {

	public MustDoGoalDAOImpl() {
		super(MustDoGoal.class);
	}
	
	public MustDoGoal getRandomMustDoGoal(List<Goal> notInGoalList){
		Date today = new Date();
		List<Long> goalIds = new ArrayList<Long>();
		for(Goal goal : notInGoalList){
			goalIds.add(goal.getId());
		}
		DetachedCriteria dc = DetachedCriteria.forClass(MustDoGoal.class);
		dc.add(Restrictions.not(Restrictions.in("goal.id", goalIds)));
		dc.add(Restrictions.le("beginDate", today));
		dc.add(Restrictions.ge("endDate", today));
		getRandomEntities(dc);
		getByStatus(dc, DataStatus.USING);
		
		List<MustDoGoal> list = findByCriteria(dc, 0, 1);
		
		if(!list.isEmpty()){
			return list.get(0);
		}
		
		return null;
	}
}
